package com.woniu.bh.beautysalon.web.service.impl;


import cn.hutool.crypto.digest.HMac;
import cn.hutool.crypto.digest.HmacAlgorithm;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.bh.beautysalon.model.dto.*;
import com.woniu.bh.beautysalon.model.params.*;
import com.woniu.bh.beautysalon.web.common.DateUtils;
import com.woniu.bh.beautysalon.web.dao.*;
import com.woniu.bh.beautysalon.web.service.IBeautysalonService;
import com.woniu.bh.common.core.domain.advertisement.Advertisement;
import com.woniu.bh.common.core.domain.advertisement.AdvertisementPic;
import com.woniu.bh.common.core.domain.backstage.SystemMessage;
import com.woniu.bh.common.core.domain.beautysalon.Beautysalon;
import com.woniu.bh.common.core.domain.beautysalon.BeautysalonPic;
import com.woniu.bh.common.core.domain.beautysalon.BeautysalonQualificationsPic;
import com.woniu.bh.common.core.domain.beautysalon.BeautysalonTechnicianMessage;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cxb
 * @since 2020-10-13
 */
@Service
public class BeautysalonServiceImpl extends ServiceImpl<BeautysalonMapper, Beautysalon> implements IBeautysalonService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private BeautysalonMapper beautysalonMapper;

    @Autowired
    private BeautysalonPicMapper beautysalonPicMapper;

    @Autowired
    private BeautysalonQualificationsPicMapper beautysalonQualificationsPicMapper;

    @Autowired
    private BeautysalonTechnicianMessageMapper beautysalonTechnicianMessageMapper;

    @Autowired
    private TechnicianMapper technicianMapper;

    @Autowired
    private SystemMessageMapper systemMessageMapper;

    @Autowired
    private AdvertisementMapper advertisementMapper;

    @Autowired
    private AdvertisementPicMapper advertisementPicMapper;

    private byte[] key = "password".getBytes();

    /**
     * @description: 密码登录校验
     * @param: loginByPassParam
     * @return: int
     */
    @Override
    public int login(LoginByPassParam loginByPassParam) {
        QueryWrapper<Beautysalon> beautysalonQueryWrapper = new QueryWrapper<>();
        //加密
        String pass = loginByPassParam.getPass();
        HMac mac = new HMac(HmacAlgorithm.HmacSHA256, key);
        String password = mac.digestHex(pass);
        beautysalonQueryWrapper.eq("beautysalon_phone", loginByPassParam.getPhone()).eq("beautysalon_password", password);
        Beautysalon beautysalon = beautysalonMapper.selectOne(beautysalonQueryWrapper);
        if (beautysalon == null) {
            return -1;
        }
        return beautysalon.getBeautysalonId();

    }

    /**
     * @description: 注册前查重
     * @param: phone
     * @return: int
     */
    @Override
    public int beforeRegister(String phone) {
        QueryWrapper<Beautysalon> beautysalonQueryWrapper = new QueryWrapper<>();
        beautysalonQueryWrapper.eq("beautysalon_phone", phone);
        Beautysalon beautysalon = beautysalonMapper.selectOne(beautysalonQueryWrapper);
        if (beautysalon == null) {
            return -1;
        }
        return beautysalon.getBeautysalonId();
    }

    /**
     * @description: 注册
     * @param: phone
     * @param: pass
     * @return: void
     */
    @Override
    public void register(String phone, String pass) {
        Beautysalon beautysalon = new Beautysalon();
        beautysalon.setBeautysalonPhone(phone);
        //加密
        HMac mac = new HMac(HmacAlgorithm.HmacSHA256, key);
        String password = mac.digestHex(pass);
        beautysalon.setBeautysalonPassword(password);
        beautysalonMapper.insert(beautysalon);
    }


    /**
     * @description: 美容馆图片
     * @param: fileUrl
     * @return: void
     */
    @Override
    public void uploadPics(String fileUrl, Integer beautysalonId) {
        BeautysalonPic beautysalonPic = new BeautysalonPic();
        beautysalonPic.setBeautysalonPicAddress(fileUrl);
        beautysalonPic.setBeautysalonPicBeautysalonId(beautysalonId);
        beautysalonPic.setBeautysalonPicUptime(System.currentTimeMillis());
        beautysalonPicMapper.insert(beautysalonPic);
    }

    /**
     * @description: 上传资质图片
     * @param: fileUrl
     * @param: beautysalonId
     * @return: void
     */
    @Override
    public void qualificationsPics(String fileUrl, Integer beautysalonId) {
        BeautysalonQualificationsPic bqp = new BeautysalonQualificationsPic();
        bqp.setBeautysalonQualificationsPicAddress(fileUrl);
        bqp.setBeautysalonQualificationsPicBeautysalonId(beautysalonId);
        bqp.setBeautysalonQualificationsPicUptime(System.currentTimeMillis());
        beautysalonQualificationsPicMapper.insert(bqp);
    }


    /**
     * @description: 美容馆信息查询
     * @param: beautysalonId
     * @return: BeautysalonDto
     */
    @Override
    public BeautysalonDto getInfo(Integer beautysalonId) {

        //查询美容馆基本信息
        Beautysalon beautysalon = beautysalonMapper.selectById(beautysalonId);
        /*if (!"2".equals(beautysalon.getBeautysalonStatus())) {
            return null;
        }*/
        //查询美容馆基本图片
        QueryWrapper<BeautysalonPic> beautysalonPicQueryWrapper = new QueryWrapper<>();
        beautysalonPicQueryWrapper.eq("beautysalon_pic_beautysalon_id", beautysalonId);
        List<BeautysalonPic> beautysalonPics = beautysalonPicMapper.selectList(beautysalonPicQueryWrapper);
        List<BeautysalonPic> beautysalonPicArrayList = new ArrayList<>(beautysalonPics);
        //查询美容馆执照图片
        QueryWrapper<BeautysalonQualificationsPic> beautysalonQualificationsPicQueryWrapper = new QueryWrapper<>();
        beautysalonQualificationsPicQueryWrapper.eq("beautysalon_qualifications_pic_beautysalon_id", beautysalonId);
        List<BeautysalonQualificationsPic> beautysalonQualificationsPics = beautysalonQualificationsPicMapper.selectList(beautysalonQualificationsPicQueryWrapper);
        List<BeautysalonQualificationsPic> beautysalonQualificationsPicArrayList = new ArrayList<>(beautysalonQualificationsPics);
        BeautysalonDto beautysalonDto = new BeautysalonDto();
        BeanUtils.copyProperties(beautysalon, beautysalonDto);
        beautysalonDto.setBeautysalonPic(beautysalonPicArrayList);
        beautysalonDto.setBeautysalonQualificationsPic(beautysalonQualificationsPicArrayList);
        return beautysalonDto;
    }

    /**
     * @description: 申请信息存入数据库
     * @param: beautysalon
     * @return: int
     */
    @Override
    public int application(Beautysalon beautysalon) {
        return beautysalonMapper.updateById(beautysalon);
    }

    /**
     * @description: 美容馆消息查询
     * @param: beautysalonId
     * @return: java.util.List<com.woniu.project.beautysalon.dto.MessageDto>
     */
    @Override
    public List<MessageDto> getMessage(Integer beautysalonId) {
        //美容师和美容馆信息查询
        QueryWrapper<BeautysalonTechnicianMessage> messageQueryWrapper = new QueryWrapper<>();
        messageQueryWrapper.eq("beautysalon_technician_message_accept_beautysalon_id", beautysalonId).orderByDesc("beautysalon_technician_message_time");
        List<BeautysalonTechnicianMessage> beautysalonTechnicianMessages = beautysalonTechnicianMessageMapper.selectList(messageQueryWrapper);
        ArrayList<MessageDto> messageDtos = new ArrayList<>();
        MessageDto messageDto = null;
        for (BeautysalonTechnicianMessage beautysalonTechnicianMessage : beautysalonTechnicianMessages) {
            messageDto = new MessageDto();
            messageDto.setContent(beautysalonTechnicianMessage.getBeautysalonTechnicianMessageContent());
            String technicianName = technicianMapper.selectById(beautysalonTechnicianMessage.getBeautysalonTechnicianMessageSendTechnicainId()).getTechnicianName();
            messageDto.setName(technicianName);
            Long messageTime = beautysalonTechnicianMessage.getBeautysalonTechnicianMessageTime();
            Date date = new Date(messageTime);
            String time = DateUtils.dateToDateString(date);
            messageDto.setTime(time);
            messageDtos.add(messageDto);
        }
        //系统消息查询
        QueryWrapper<SystemMessage> systemMessage = new QueryWrapper<>();
        systemMessage.eq("system_message_accepter_id", beautysalonId).eq("system_message_type", 3).orderByDesc("system_message_creat_time");
        List<SystemMessage> systemMessages = systemMessageMapper.selectList(systemMessage);
        for (SystemMessage systemMessage1 : systemMessages) {
            messageDto = new MessageDto();
            messageDto.setName("系统消息");
            messageDto.setContent(systemMessage1.getSystemMessageContent());
            Long systemMessageCreatTime = systemMessage1.getSystemMessageCreatTime();
            Date date = new Date(systemMessageCreatTime);
            String time = DateUtils.dateToDateString(date);
            messageDto.setTime(time);
            messageDtos.add(messageDto);
        }
        return messageDtos;
    }

    /**
     * @description: 分页查询所有有消费的用户
     * @param: pageIndex
     * @param: pageSize
     * @param: sex
     * @param: beautysalonId
     * @param: beforeAge
     * @param: afterAge
     * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.woniu.project.beautysalon.dto.CustomerDto>
     */
    @Override
    public IPage<CustomerDto> pageQuery(int pageIndex, int pageSize, String sex, Integer beautysalonId, Integer beforeAge, Integer afterAge) {
        Page<CustomerDto> page = new Page<>(pageIndex, pageSize);
        return beautysalonMapper.pageQuery(page, beautysalonId, sex, beforeAge, afterAge);
    }

    /**
     * @description: 新增广告
     * @param: advertisementParam
     * @param: beautysalonId
     * @return: void
     */
    @Override
    public void addNewAd(AdvertisementParam advertisementParam, Integer beautysalonId) {
        //新增数据
        Advertisement advertisement = new Advertisement();
        BeanUtils.copyProperties(advertisementParam, advertisement);
        advertisement.setAdvertisementBeautysalonId(beautysalonId);
        advertisement.setAdvertisementTime(System.currentTimeMillis());
        advertisement.setAdvertisementRentStartTime(advertisementParam.getAdvertisementRentStartTime().getTime());
        advertisement.setAdvertisementRentEndTime(advertisementParam.getAdvertisementRentEndTime().getTime());
        advertisement.setAdvertisementStatus(0);
        advertisement.setAdvertisementOnShelf(2);
        advertisementMapper.insert(advertisement);
        //获取自增主键
        Integer advertisementId = advertisement.getAdvertisementId();
        Set<Object> advertisementPics = stringRedisTemplate.opsForHash().keys("advertisementPic");
        //存图片
        for (int i = 0; i < advertisementPics.size(); i++) {
            String advertisementPic1 = Objects.requireNonNull(stringRedisTemplate.opsForHash().get("advertisementPic", "pic" + i)).toString();
            AdvertisementPic advertisementPic2 = new AdvertisementPic();
            advertisementPic2.setAdvertisementPicAddress(advertisementPic1);
            advertisementPic2.setAdvertisementPicAdvertisementId(advertisementId);
            advertisementPic2.setAdvertisementPicUptime(System.currentTimeMillis());
            advertisementPicMapper.insert(advertisementPic2);
        }
    }

    /**
     * @description: 分页条件查询美容馆基本信息
     * @param: conditionedQueryParam
     * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.woniu.bh.beautysalon.model.dto.BeautysaloBaseInfoDto>
     */
    @Override
    public IPage<BeautysalonBaseInfoDto> getBaseInfo(ConditionedQueryParam conditionedQueryParam) {

        Page<BeautysalonBaseInfoDto> page = new Page<>(conditionedQueryParam.getPageIndex(), conditionedQueryParam.getPageSize());
        return beautysalonMapper.getBaseInfo(page, conditionedQueryParam.getBeforeDate(), conditionedQueryParam.getAfterDate(), conditionedQueryParam.getBeautysalonName(), conditionedQueryParam.getBeautysalonStatus());
    }

    /**
     * @description: 删除场馆
     * @param: beautysalonId
     * @return: int
     */
    @Override
    public int beautysalonDel(Integer beautysalonId) {
        Beautysalon beautysalon = new Beautysalon();
        beautysalon.setBeautysalonId(beautysalonId);
        beautysalon.setBeautysalonDeleteStatus(1);
        return beautysalonMapper.updateById(beautysalon);
    }

    /**
     * @description: 审核美容馆通过
     * @param: beautysalonId
     * @return: int
     */
    @Override
    public int auditPass(Integer beautysalonId) {
        //修改状态
        Beautysalon beautysalon = new Beautysalon();
        beautysalon.setBeautysalonId(beautysalonId);
        beautysalon.setBeautysalonStatus("2");
        return beautysalonMapper.updateById(beautysalon);
    }

    /**
     * @description: 审核未通过
     * @param: beautysalonCheckResultParam
     * @return: int
     */
    @Override
    public int auditRejected(BeautysalonCheckResultParam beautysalonCheckResultParam) {
        //修改状态
        Beautysalon beautysalon = new Beautysalon();
        beautysalon.setBeautysalonId(beautysalonCheckResultParam.getBeautysalonId());
        beautysalon.setBeautysalonStatus("3");
        beautysalonMapper.updateById(beautysalon);

        //生成消息
        SystemMessage systemMessage = new SystemMessage();
        systemMessage.setSystemMessageContent(beautysalonCheckResultParam.getContent());
        systemMessage.setSystemMessageAccepterId(beautysalonCheckResultParam.getBeautysalonId());
        systemMessage.setSystemMessageCreatTime(System.currentTimeMillis());
        systemMessage.setSystemMessageType(3);
        systemMessage.setSystemMessageIsChecked(0);
        systemMessageMapper.insert(systemMessage);
        return systemMessage.getSystemMessageId();
    }

    /**
     * @description: 我的广告
     * @param: beautysalonId
     * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.woniu.bh.beautysalon.model.dto.MyAdDto>
     */
    @Override
    public IPage<MyAdDto> getAllAd(MyAdParam myAdParam, Integer beautysalonId) {
        Page<MyAdDto> myAdDtoPage = new Page<>(myAdParam.getPageIndex(), myAdParam.getPageSize());
        return beautysalonMapper.getAllAds(myAdDtoPage,myAdParam.getAdvertisementStatus(),myAdParam.getAdvertisementTheme(),myAdParam.getAfterDate(),myAdParam.getBeforeDate(),beautysalonId);

    }
    /**
     * @description: 重置密码
     * @param: phone
     * @param: pass
     * @return: void
     */
    @Override
    public void restPass(Integer beautysalonId, String pass) {
        Beautysalon beautysalon = new Beautysalon();
        beautysalon.setBeautysalonId(beautysalonId);
        beautysalon.setBeautysalonPassword(pass);
        beautysalonMapper.updateById(beautysalon);
    }
}
